package co.id.vivastor.pantau;

import co.id.vivastor.pantau.util.Global;
import co.id.vivastor.pantau.util.SMSSender;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class LogistikActivity extends Activity {
	public static final String IDENTIFIER = "LOGISTIK";
	private int _selectedJenisBarang;
	private int _selectedStatusKirim;
	private int _selectedKondisiBarang;
	private String _keterangan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logistik);
		_setOtherViews();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_logistik, menu);
		return true;
	}
	
	private void _setOtherViews() {
		//Icon Clickable goto home menu:
		ImageView icon = (ImageView) findViewById(R.id.imageView1);
		Global.setImageViewButtonListener(icon, LogistikActivity.this, MainActivity.class);
				
		//Set Spinner Jenis Barang
		Spinner spinnerJenisBarang = (Spinner) findViewById(R.id.spinner_jenis_barang);
		ArrayAdapter<CharSequence> adapterJenisBarang = ArrayAdapter.createFromResource(this, R.array.logistik_jenis_barang, R.layout.spinner_layout);
		adapterJenisBarang.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerJenisBarang.setAdapter(adapterJenisBarang);
		spinnerJenisBarang.setOnItemSelectedListener(
				new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						//Toast.makeText(LogistikActivity.this, arg0.getItemIdAtPosition(arg2)+arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show();
						LogistikActivity.this._selectedJenisBarang = (int)arg0.getItemIdAtPosition(arg2);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						
					}
				}
		);
		
		//Set Spinner Status Pengiriman
		Spinner spinnerStatusKirim = (Spinner) findViewById(R.id.spinner_status_kirim);
		ArrayAdapter<CharSequence> adapterStatusKirim = ArrayAdapter.createFromResource(this, R.array.logistik_status_kirim, R.layout.spinner_layout);
		adapterStatusKirim.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerStatusKirim.setAdapter(adapterStatusKirim);
		spinnerStatusKirim.setOnItemSelectedListener(
				new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						//Toast.makeText(LogistikActivity.this, arg0.getItemIdAtPosition(arg2)+arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show();
						LogistikActivity.this._selectedStatusKirim = (int)arg0.getItemIdAtPosition(arg2);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				}
		);
		
		//Set Spinner Kondisi Barang
		Spinner spinnerKondisiBarang = (Spinner) findViewById(R.id.spinner_kondisi_barang);
		ArrayAdapter<CharSequence> adapterKondisiBarang = ArrayAdapter.createFromResource(this, R.array.logistik_kondisi_barang, R.layout.spinner_layout);
		adapterKondisiBarang.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerKondisiBarang.setAdapter(adapterKondisiBarang);
		spinnerKondisiBarang.setOnItemSelectedListener(
				new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						//Toast.makeText(LogistikActivity.this, arg0.getItemIdAtPosition(arg2)+arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_SHORT).show();
						LogistikActivity.this._selectedKondisiBarang = (int)arg0.getItemIdAtPosition(arg2);
					}
					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				}
		);
		
		//Listener Send Button
		Button btnSendSMS = (Button) findViewById(R.id.buttonSend);
		btnSendSMS.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Cek setting udah terisi:
				String kodeWilayah = Global.getData(getApplicationContext(), Global.KODE_WILAYAH, Global.DEFAULT_VALUE);
				String nomorTPS = Global.getData(getApplicationContext(), Global.NOMOR_TPS, Global.DEFAULT_VALUE);
				String SMSCenterNo = Global.getData(getApplicationContext(), Global.SMS_CENTER_NO, Global.DEFAULT_VALUE);
				
				if (kodeWilayah == Global.DEFAULT_VALUE || nomorTPS == Global.DEFAULT_VALUE || SMSCenterNo == Global.DEFAULT_VALUE ||
						kodeWilayah == "" || nomorTPS == "" || SMSCenterNo == "") {
					//Toast.makeText(getApplicationContext(), "Maaf, Setting belum terkonfigurasi", Toast.LENGTH_SHORT).show();
					Global.showDialog(LogistikActivity.this, "Konfigurasi", "Maaf, setting belum terkonfigurasi. Cek halaman Setting.", "Setting");
				} else {
					EditText  keterangan = (EditText) findViewById(R.id.editText_keterangan);
					LogistikActivity.this._keterangan = keterangan.getText().toString();
					
					SMSSender smsSender = new SMSSender(LogistikActivity.this);
					String message = LogistikActivity.IDENTIFIER+" "+kodeWilayah+" "+nomorTPS+" "+LogistikActivity.this._selectedJenisBarang+" "+LogistikActivity.this._selectedStatusKirim+" "+LogistikActivity.this._selectedKondisiBarang+" "+LogistikActivity.this._keterangan;
					smsSender.sendSMS(SMSCenterNo, message);
				}
			}
		});
		
	}
	

}
